package br.com.maps.ui;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import br.com.maps.contacts.Contact;
import br.com.maps.ramais.R;

public class ContactAdapter extends BaseAdapter {

	private List<Contact> contacts;
	private Context context;

	public ContactAdapter(Context context, List<Contact> contacts) {
		this.context = context;
		this.contacts = contacts;
	}

	public int getCount() {
		return this.contacts.size();
	}

	public Object getItem(int position) {
		return this.contacts.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		final Contact contact = this.contacts.get(position);
		LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.contact_adapter, parent, false);
		TextView name = (TextView) v.findViewById(R.id.nome);
		name.setText(contact.getName());
		ImageButton goButton = (ImageButton) v.findViewById(R.id.go_button);
		goButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Toast.makeText(ContactAdapter.this.context, "Clicou para ver detalhes do contato: " + contact.getName(), Toast.LENGTH_SHORT).show();
			}
		});
		ImageButton phoneButton = (ImageButton) v.findViewById(R.id.phone_button);
		phoneButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Toast.makeText(ContactAdapter.this.context, "Clicou para ligar para o contato: " + contact.getName(), Toast.LENGTH_SHORT).show();
			}
		});
		return v;
	}

	public void setContacts(List<Contact> contacts) {
		this.contacts = contacts;
	}
}
